package com.taiyi.renaissancemall.ui.webview

import android.os.Build
import android.os.Bundle
import android.webkit.*
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.bean.AgreementsBean
import kotlinx.android.synthetic.main.activity_web_view.*
import kotlinx.android.synthetic.main.base_top_white_background_layout.*

class WebViewActivity : BaseActivity<WebViewView, WebViewPresent>(), WebViewView {
    var isWhere = -1

    override fun getLayoutId(): Int = R.layout.activity_web_view

    override fun initPresenter(): WebViewPresent = WebViewPresent()

    override fun initViews(savedInstanceState: Bundle?) {
        backIv.setOnClickListener { finish() }
        isWhere = intent.getIntExtra("isWhere", -1)
        if (isWhere == 1) {
            titleTv.setText(R.string.registerAgreement)
        } else if (isWhere == 2) {
            titleTv.setText(R.string.commonProblem)
        } else if (isWhere == 3) {
            titleTv.setText(R.string.aboutUs)
        } else if (isWhere == 4) {

        }
        var settings = webView.settings
        settings.javaScriptEnabled = true
        settings.setSupportZoom(false)
        settings.builtInZoomControls = false
        settings.displayZoomControls = false
        settings.domStorageEnabled = true
        settings.useWideViewPort = true;
        settings.loadWithOverviewMode = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW; }
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(top).init()
    }

    override fun initData() {
        showProgress()
        presenter.getAgreement()
    }

    override fun initEvent() {
    }

    override fun onFailure(msg: String?) {
        toast(msg)
    }

    override fun getAgreementSuccess(agreementsBean: AgreementsBean?) {
        hideProgress()
        if (isWhere == 1) {
            var htmlData = agreementsBean?.zhucetext?.let { getHtmlData(it) }
            webView.loadDataWithBaseURL(null, htmlData, "text/html", "utf-8", null)
        } else if (isWhere == 2) {
            var htmlData = agreementsBean?.changjianpel?.let { getHtmlData(it) }
            webView.loadDataWithBaseURL(null, htmlData, "text/html", "utf-8", null)
        } else if (isWhere == 3) {
            var htmlData = agreementsBean?.guanyuwe?.let { getHtmlData(it) }
            webView.loadDataWithBaseURL(null, htmlData, "text/html", "utf-8", null)
        }
    }

    private fun getHtmlData(bodyHTML: String): String? {
        val head = ("<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:100%; height:auto;}*{margin:0px;}</style>"
                + "</head>")
        return "<html>$head<body>$bodyHTML</body></html>"
    }

}
